package com.sihan.swvp.action.jc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.jc.JcBzjs;
import com.sihan.swvp.service.jc.JcBzService;
import com.sihan.swvp.service.jc.JcBzjsService;
import com.sihan.swvp.service.jc.bzzy.BzzyMajorService;
import com.sihan.swvp.service.sys.LogService;

@Controller
@RequestMapping("/jc/JcBzjsAction")
public class JcBzjsAction extends BaseAction{

	@Autowired
	private JcBzjsService jcBzjsService;
	@Autowired
	private JcBzService jcBzService;
	@Autowired
	private BzzyMajorService bzzyMajorService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping("/getJcBzjs")
	public Object getJcBzjs(@Param("bzjsCode")String bzjsCode,@Param("jcBzId")Integer jcBzId,@Param("bzjsName")String bzjsName){
		try {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("bzjsCode", bzjsCode);
			params.put("bzjsName", bzjsName);
			params.put("jcBzId", jcBzId);
			return jcBzjsService.getJcBzjs(params);
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/getBdzBzTree") 
	public Object getBdzBzTree(@Param("node")String node){
		Map<String, Object> params = new HashMap<String, Object>();
		List<Map<String, Object>> erlist = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> resultlist=null;
		
		if(node.equals("0")){//父节点
			erlist=bzzyMajorService.getBzzyMajor(null, null, null);//得到专业数据
			resultlist= new ArrayList<Map<String,Object>>();
			for (int x = 0; x < erlist.size(); x++) {//父节点循环
				 params.put("bzzyMajorId", erlist.get(x).get("id"));
				 List<Map<String, Object>> listData=jcBzService.getJcBzTree(params);//得到班组数据
				 Map<String, Object> twoMap=new HashMap<String, Object>();
				 twoMap.put("text", erlist.get(x).get("bzzyName"));
				 twoMap.put("id", erlist.get(x).get("id"));
				 twoMap.put("iconCls", "icon-open");
				 List<Map<String, Object>> onelist=new ArrayList<Map<String,Object>>();
				 for (int i = 0; i < listData.size(); i++) {//子节点循环
					 Map<String, Object> mapBz=new HashMap();
					 mapBz.put("text", listData.get(i).get("text"));
					 mapBz.put("pid", listData.get(i).get("id"));
					 mapBz.put("leaf",true);
					 onelist.add(mapBz);
				}
				 twoMap.put("children", onelist);
				 resultlist.add(twoMap);
				 
			}
		}
		return resultlist;
	}
	
	/**
	 * 查询班组的数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJcBz")
	public Object getJcBz(@Param("bzName")String bzName){
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("bzName", bzName);
		return jcBzService.getJcBz(param,null,null);
	}
	
	
	@ResponseBody
	@RequestMapping("/addJcBzjs")
	public Object addJcBzjs(JcBzjs bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcBzjsService.getJcBzjsValidate(null, bean.getBzjsName(),null,bean.getJcBzId());
			boolean isCodeOk=jcBzjsService.getJcBzjsValidate(bean.getBzjsCode(), null,null,bean.getJcBzId());
			if(isDsdwNameOk){
				return failMsg("班组角色已存在");
			}else if(isCodeOk){
				return failMsg("班组角色编码已存在");
			}
			jcBzjsService.addJcBzjs(bean);
			logService.addUserLog("添加班组角色", "班组角色", getLoginUser().getId(), "添加班组角色："+bean.getBzjsName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加班组角色", "班组角色", getLoginUser().getId(), "添加班组角色："+bean.getBzjsName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/updateJcBzjs")
	public Object updateJcBzjs(JcBzjs bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcBzjsService.getJcBzjsValidate(null, bean.getBzjsName(),bean.getId(),bean.getJcBzId());
			boolean isCodeOk=jcBzjsService.getJcBzjsValidate(bean.getBzjsCode(), null,bean.getId(),bean.getJcBzId());
			if(isDsdwNameOk){
				return failMsg("班组角色已存在");
			}else if(isCodeOk){
				return failMsg("班组角色编码已存在");
			}
			jcBzjsService.updateJcBzjs(bean);
			logService.addUserLog("修改班组角色", "班组角色", getLoginUser().getId(), "修改班组角色："+bean.getBzjsName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改班组角色", "班组角色", getLoginUser().getId(), "修改班组角色："+bean.getBzjsName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteJcBzjs")
	public Object deleteJcBzjs(@Param("id")Integer id,String bzjsName){
		try {
			jcBzjsService.deleteJcBzjs(id);
			logService.addUserLog("删除班组角色", "班组角色", getLoginUser().getId(), "删除班组角色："+bzjsName,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除班组角色", "班组角色", getLoginUser().getId(), "删除班组角色："+bzjsName,getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
}
